/*************************************************************************
	> File Name: list.c
	> Author:jason 
	> Mail:3297294149@qq.com 
	> Created Time: Sat 16 Sep 2023 03:45:16 PM CST
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct Vector{
    int *data;
    int size;
    int length;

}Vector;
Vector *init(int n)
{
    Vector *vec=(Vector *)malloc(sizeof(Vector));
    vec->data=(int *)malloc(sizeof(int)*n);
    vec->length=0;
    vec->size=n;
    return vec;
}
void clear(Vector *vec)
{
    if(vec==NULL) return ;
    free(vec->data);
    free(vec);
    return ;
}
int expand(Vector *vec)
{
    vec->size<<=1;
    vec->data=(int *)realloc(vec->data,sizeof(int)*vec->size);
    return 1;
}
int insert(Vector *vec,int ind,int x)
{
    if(vec==NULL) return 0;
    if(ind <0 || ind > vec->length) return 0;
    if(vec->length==vec->size){
        if(!expand(vec)) return 0;
        printf("expand successfully!size=%d\n",vec->size);
    }
    for(int i=vec->length;i>ind;i--)
    {
        vec->data[i]=vec->data[i-1];
    }
    vec->data[ind]=x;
    vec->length+=1;
    return 1;
}
int erase(Vector *vec,int ind)
{
    if(vec==NULL) return 0;
    if(ind <0 || ind >vec->length) return 0;
    for(int i=ind+1;i<vec->length;i++)
    {
        vec->data[i-1]=vec->data[i];
    }
    vec->length-=1;
    return 1;
}
void output(Vector *vec)
{
    if(vec==NULL) return ;
    for(int i=0;i<vec->length;i++)
    {
        i && printf(", ");
        printf("%d",vec->data[i]);
    }
    printf("]\n");
    return ;
}
int main()
{
    #define max_op 1
    srand(time(0));
    Vector *vec=init(max_op);
    int op,ind,val;
    for(int i=0;i<max_op;i++)
    {
        op=rand()%4;
        ind=rand()%(vec->length+3)-1;
        val=rand()%100;
        switch(op)
        {
            case 0:
            case 1:
            case 2:
            {
                printf("insert %d to %d to Vector(%d)=%d\n",val,ind,ind,insert(vec,ind,val));
            }break;
            case 3:{
                printf("delete items at %d in Vector(%d)=%d\n",ind,ind,erase(vec,ind));
            }break;
        }
        output(vec);
        printf("\n");
    }
    clear(vec);
    return 0;
}
